/**
 * Created by Administrator on 2015/12/7.
 */
var net = require("net");
var server = net.createServer();

var socket = [];

server.on("connection", function (socket) {
    console.log("一个客户端已经连接上来了");
    socket.push(socket);

    socket.on("data", function (data) {
        console.log("receive data :" + data);
        //  遍历发送对应的数据
        sockets.forEach(function (otherSocket) {
            if (otherSocket != socket) otherSocket.write(data);
        });
    });
    // 完成关闭
    socket.on("close", function () {
        console.log("connection closed");
        var index = sockets.indexOf(socket);
        // 移除集合中的这个位置的内容
        sockets.splice(index, 1);
    });
});


server.on("error", function (err) {
    console.log("you have an error");
})

server.on("close", function () {
    console.log("server closed");
});
//
//server.on("listen",function(){
//    console.log("start  listen!");
//})

server.listen(4001);